#include "Button.h"
#include "Button.h"
#include "Button.h"
#include "Button.h"
#include "UIRender.h"
#include "Theme.h"

void Button::Create(Window* window,vec2 pos, vec2 size, UINT32 id)
{
	Control::Create(window, pos, size, id);
}
tstring Button::GetClassName()
{
	return _T("Button");
}

//void Button::onMouseEnter(int x, int y)
//{
//	if (HitTest(vec2(x, y)))
//	{
//		bHovered = true;
//	}
//}
//
//void Button::onMouseLeave(int x, int y)
//{
//	if (!HitTest(vec2(x, y)))
//	{
//		bHovered = false;
//	}
//}

void Button::onMouseButtonDown(int button, int x, int y)
{
	if (HitTest(vec2((float)x, (float)y)))
	{
		bPressed = true;
	}
}

void Button::onMouseButtonUp(int button, int x, int y)
{
	if (bPressed)
		bPressed = false;
}



void Button::onMouseMove(int x, int y)
{
	bHovered = HitTest(vec2(x, y));
}


bool Button::SetAttribute(const char* attrName, const char* attrVal)
{
	__super::SetAttribute(attrName, attrVal);

	if (strcmp(attrName, "bkcolor") == 0)
	{
		sClrBK = attrVal;
		/*int x, y,z;
		sscanf_s(attrVal, "%d,%d,%d", &x, &y,&z);
		this->clrBk.r = x;
		this->clrBk.g = y;
		this->clrBk.b = z;*/
		return true;
	}
	else if (strcmp(attrName, "content") == 0)
	{
		text = attrVal;
		return true;
	}
	else if (strcmp(attrName, "hoverColor") == 0)
	{
		sClrHover = attrVal;
		return true;
	}
	else if (strcmp(attrName, "foreColor") == 0)
	{
		sClrFore = attrVal;
		return true;
	}
	else if (strcmp(attrName, "pressedColor") == 0)
	{
		sClrPressed = attrVal;
		return true;
	}
	
	return false;
}

bool Button::OnInit()
{
	Control::OnInit();
	return true;
}
void Button::OnDestroy()
{
	Control::OnDestroy();
}
void Button::Draw()
{
	Control::Draw();
	glLineWidth(2);
	Theme::ITheme* theme = Theme::CustomTheme::GetInstance();

	if (bPressed) {
		UIRender::DrawFillRect(pos.x.val, pos.y.val, size.x.val, size.y.val, sClrPressed);//theme->GetPressColor());
	}
	else if (bHovered)
	{
		UIRender::DrawFillRect(pos.x.val, pos.y.val, size.x.val, size.y.val, this->sClrHover);// theme->GetHoverColor());
	}
	else {
		UIRender::DrawFillRect(pos.x.val, pos.y.val, size.x.val, size.y.val, this->sClrFore);// theme->GetBackColor());
	}
	UIRender::DrawEdgeRect(pos.x.val, pos.y.val, size.x.val, size.y.val, theme->GetBorderColor());
	UIRender::DrawString(vec2(pos.x.val, pos.y.val), text);
}
void Button::Resize(int w, int h)
{
	Control::Resize(w, h);
}
